package com.ewanghuiju.app.presenter.my;

import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.my.MyContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.OrderStatusResponBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.bean.response.VersionResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.MyHttpResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/16
 */
public class MyPresenter extends RxPresenter<MyContract.View> implements MyContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public MyPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getUserInfo() {
        //获取用户信息
        addSubscribe(mDataManager.getUserInfo()
                .compose(RxUtil.<MyHttpResponse<UserInfoResponBean>>rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<UserInfoResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<UserInfoResponBean> userInfo) {
                        if (mView != null) {
                            if (userInfo.getIncludeNull() != null) {
                                mDataManager.insertUserInfoResponBean(userInfo.getIncludeNull());
                                //刷新信息
                                RxBus.getDefault().post(new SendEvent("", Constants.TYPE_TO_REFRESH_DATA));
                            }
                            mView.showUserInfo(userInfo.getIncludeNull());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mView != null)
                            mView.showUserInfoError();
                    }
                })
        );
    }

    @Override
    public void getData() {
        //刷新获取数据
        UserInfoResponBean userInfoResponBean = mDataManager.getUserInfoResponBean();
        if (userInfoResponBean == null) {
            getUserInfo();
        } else {
            mView.showUserInfo(userInfoResponBean);
        }
    }

    /**
     * 清除用户信息
     */
    @Override
    public void clearUserInfo() {
        if (mDataManager != null)
            mDataManager.deleteUserInfoResponBean();
    }

    @Override
    public void getOsstoken(String sessionname) {
        addSubscribe(mDataManager.getOsstoken(sessionname)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if (mView != null) {
                            mView.showOsstoken(optional.getIncludeNull());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    /**
     * 设置头像
     *
     * @param avater
     */
    @Override
    public void settingAvater(String avater) {
        //设置昵称 type == 1
        addSubscribe(mDataManager.userinfo_edit("1", avater, "")
                .compose(RxUtil.<MyHttpResponse<BaseResponse>>rxSchedulerHelper())
                .compose(RxUtil.<BaseResponse>handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> baseResponse) {
                        if (mView != null)
                            mView.settingAvaterSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void bindWx(String unionid, String headimgurl, String nickname) {
        //绑定微信
        addSubscribe(mDataManager.mobileWx(unionid, headimgurl, nickname)
                .compose(RxUtil.<MyHttpResponse<BaseResponse>>rxSchedulerHelper())
                .compose(RxUtil.<BaseResponse>handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> baseResponse) {
                        if (mView != null)
                            mView.bindWxSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void unBindWx(String wx) {
        //解绑微信
        addSubscribe(mDataManager.wxDel("1")
                .compose(RxUtil.<MyHttpResponse<BaseResponse>>rxSchedulerHelper())
                .compose(RxUtil.<BaseResponse>handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> baseResponse) {
                        if (mView != null)
                            mView.unBindWxSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void taokeGetlbimg(int type) {
        //获取广告
        addSubscribe(mDataManager.taokeGetlbimg(type + "")
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        mView.showLbImg(type, optional.getIncludeNull().getBanner());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void getUpgradeInfo(String version, String type) {
        //获取更新信息
        addSubscribe(mDataManager.getUpgradeInfo(version, type)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<VersionResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<VersionResponBean> optional) {
                        mView.showUpgradeInfo(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void getPtiorderStatus() {
        addSubscribe(mDataManager.getPtiorderStatus()
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<OrderStatusResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<OrderStatusResponBean> optional) {
                        mView.showPtiorderStatus(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }


    @Override
    public void attachView(MyContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    /**
     * 获取用户信息   --刷新用户信息
     */
    private void registerEvent() {
        addSubscribe(RxBus.getDefault().toFlowable(SendEvent.class)
                .compose(RxUtil.<SendEvent>rxSchedulerHelper())
                .filter(new Predicate<SendEvent>() {
                    @Override
                    public boolean test(@NonNull SendEvent sendEvent) throws Exception {
                        return sendEvent.getType() == Constants.TYPE_TO_REFRESH_DATA;
                    }
                })
                .subscribeWith(new CommonSubscriber<SendEvent>(mView, "") {
                    @Override
                    public void onNext(SendEvent s) {
                        getData();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        registerEvent();
                    }
                })
        );
    }
}
